from variable import Variable
from operate import confirm_step, left_click, pic_click
import time
from util import go_position, go_back, go_home
from fight.fightUtil import fighting, fighting_confirm_quit


def fightOne():
    print("开始纷争战区")
    go_position("fight_one_map")
    if confirm_step("images/fight/fight_one/create_group.png"):
        print("组队中,不能打")
        go_back()
        return
    # 地图序号
    timer = 0
    # 战斗次数
    number = Variable.fight_one["number"]
    # 首次进入
    once = True
    for i in [[2.5, 2.9], [1.5, 3.1], [8.4, 1.9], [2.3, 1.5], [1.3, 1.45]]:
        go_position("fight_one_map")
        timer += 1
        print(f"进入地图{timer}")
        left_click(Variable.width / i[0], Variable.height / i[1])
        if confirm_step("images/fight/fight_one/fight_one_tip.png"):
            print("正在解锁")
            left_click(Variable.width / 1.65, Variable.height / 1.35)
            left_click(Variable.width / i[0], Variable.height / i[1])
        if confirm_step(
            "images/fight/fight_one/score.png",
            confidence=0.6,
            w=355,
            h=130,
            bl=0,
            bt=185,
        ):
            print(f"地图{timer}未开放")
            time.sleep(1)
            continue

        for index, j in enumerate([4, 2.1, 1.4]):
            print("开始第{}个".format(index + 1))
            left_click(Variable.width / j, Variable.height / 2.1)
            # 送死刷次数
            if once and number > 0:
                if confirm_step("images/fight/fight_one/auto_fight.png"):
                    for _ in range(number):
                        print(f"首通{_+1}次")
                        if _ != 0:
                            left_click(Variable.width / j, Variable.height / 2.1)
                        # 点击进入区域
                        pic_click(
                            src="images/fight/fight_one/go_area.png",
                            confirm="images/fight/choose_team.png",
                        )
                        if once:
                            pic_click("images/fight/fight_roles/队伍预设.png")
                            left_click(Variable.width / 4.9, Variable.height / 1.178)
                            once = False
                        # 点击作战开始
                        left_click(Variable.width / 1.1675, Variable.height / 1.07)
                        fighting_confirm_quit(quitSrc="images/fight/quit_fight.png")
                        time.sleep(1)
                left_click(Variable.width / j, Variable.height / 2.1)
            # 自动作战
            if pic_click(src="images/fight/fight_one/auto_fight.png", delay=3):
                go_back()
                print("自动作战完成")
                continue

            if not confirm_step("images/fight/fight_one/score0.png", 0.6):
                go_back()
                print(f"第{index + 1}个已经打了")
                continue

            # 点击进入区域
            if pic_click(
                src="images/fight/fight_one/go_area.png",
                confirm="images/fight/choose_team.png",
            ):
                # 换回第二编队
                pic_click("images/fight/fight_roles/队伍预设.png")
                left_click(Variable.width / 2.13, Variable.height / 1.185)
                # 点击作战开始
                left_click(Variable.width / 1.1675, Variable.height / 1.07)
                # 进入战斗
                fighting(quitSrc="images/fight/quit_fight.png")
                time.sleep(3)
            else:
                print("进入战斗失败")
        go_back(delay=2)
    print("纷争战区战斗完成")
    go_home()


if __name__ == "__main__":
    fightOne()
